﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Npgsql;
using System.Data;

namespace SIGEB.Modelo.moduloTipoDocumentos
{
    public class conjuntoAtributos:conjuntoBase
    {

        #region "constructores"

        public conjuntoAtributos(datosAplicacion dA)
            : base(dA)
        {
            this.numTipo = CONJATRIBUTOS;
        }

        #endregion

        #region "metodos"

        #region "imec"

        public void agregaratributo(atributo nuevoatributo)
        {
            try
            {
                compConexion.Open();
                string sql = "SELECT registrar_atributo('" + nuevoatributo.idTipoDoc + "', '" + nuevoatributo.nombre + "', " + nuevoatributo.indizable + ", " + nuevoatributo.tipo + ")";
                NpgsqlDataAdapter da = new NpgsqlDataAdapter(sql, compConexion);
                // i always reset DataSet before i do something with it.... i don't know why :-)
                ds.Reset();
                // filling DataSet with result from NpgsqlDataAdapter
                da.Fill(ds);
                // since it C# DataSet can handle multiple tables, we will select first
                dt = ds.Tables[0];
            }
            catch (Exception e)
            {
                throw new Exception(e.Message + "\nError al agregar atributo: " + nuevoatributo.codAtributo);
            }
            finally
            {
                try
                {
                    compConexion.Close();
                }
                catch
                {

                }
            }
            notificar(this.numTipo, null);
        }

        public void eliminaratributo(String codatributo)
        {
            try
            {
                compConexion.Open();
                string sql = "SELECT eliminar_atributo('" + codatributo + "')";
                NpgsqlDataAdapter da = new NpgsqlDataAdapter(sql, compConexion);
                // i always reset DataSet before i do something with it.... i don't know why :-)
                ds.Reset();
                // filling DataSet with result from NpgsqlDataAdapter
                da.Fill(ds);
                // since it C# DataSet can handle multiple tables, we will select first
                dt = ds.Tables[0];
            }
            catch (Exception e)
            {
                throw new Exception(e.Message + "\nError al eliminar atributo: " + codatributo);
            }
            finally
            {
                try
                {
                    compConexion.Close();
                }
                catch
                {

                }
            }
            notificar(this.numTipo, null);
        }

        public void modificaratributo(String codatributo, atributo nuevoatributo)
        {
            try
            {
                compConexion.Open();
                string sql = "SELECT actualizar_tipo_perfil('" + codatributo + "', '" + nuevoatributo.nombre + "')";
                NpgsqlDataAdapter da = new NpgsqlDataAdapter(sql, compConexion);
                // i always reset DataSet before i do something with it.... i don't know why :-)
                ds.Reset();
                // filling DataSet with result from NpgsqlDataAdapter
                da.Fill(ds);
                // since it C# DataSet can handle multiple tables, we will select first
                dt = ds.Tables[0];
            }
            catch (Exception e)
            {
                throw new Exception(e.Message + "\nError al editar atributo: " + codatributo);
            }
            finally
            {
                try
                {
                    compConexion.Close();
                }
                catch
                {

                }
            }
            notificar(this.numTipo, null);
        }

        public atributo obteneratributo(String codatributo)
        {
            atributo nueva = null;
            try
            {
                compConexion.Open();
                string sql = "SELECT * FROM consultar_tipo_perfil_id('" + codatributo + "')";
                NpgsqlDataAdapter da = new NpgsqlDataAdapter(sql, compConexion);
                // i always reset DataSet before i do something with it.... i don't know why :-)
                ds.Reset();
                // filling DataSet with result from NpgsqlDataAdapter
                da.Fill(ds);
                // since it C# DataSet can handle multiple tables, we will select first
                dt = ds.Tables[0];
            }
            catch (Exception e)
            {
                throw new Exception(e.Message + "\nError al obtener el atributo " + codatributo);
            }
            finally
            {
                try
                {
                    compConexion.Close();
                }
                catch
                {

                }
            }
            string[] valores;
            foreach (DataRow row in dt.Rows)
            {
                valores = datosDataSet(row, 5);
                nueva = new atributo(valores[0], valores[1], valores[3], bool.Parse(valores[4]), int.Parse(valores[2]));
            }
            return nueva;
        }

        public LinkedList<atributo> obtenerAtributos()
        {
            LinkedList<atributo> nueva = new LinkedList<atributo>();
            try
            {
                compConexion.Open();
                string sql = "SELECT * FROM consultar_tipo_perfil()";
                NpgsqlDataAdapter da = new NpgsqlDataAdapter(sql, compConexion);
                // i always reset DataSet before i do something with it.... i don't know why :-)
                ds.Reset();
                // filling DataSet with result from NpgsqlDataAdapter
                da.Fill(ds);
                // since it C# DataSet can handle multiple tables, we will select first
                dt = ds.Tables[0];
                string[] valores;
                foreach (DataRow row in dt.Rows)
                {
                    valores = datosDataSet(row, 3);
                    nueva.AddLast(new atributo(valores[0], valores[1], valores[2], bool.Parse(valores[3]), int.Parse(valores[4])));
                }
            }
            catch (Exception e)
            {
                throw new Exception(e.Message + "\nError al obtener los atributos");
            }
            finally
            {
                try
                {
                    compConexion.Close();
                }
                catch
                {

                }
            }
            return nueva;
        }

        public LinkedList<atributo> obteneratributosIdDTipoDoc(string id)
        {
            LinkedList<atributo> nueva = new LinkedList<atributo>();
            try
            {
                compConexion.Open();
                string sql = "SELECT * FROM consultar_atributo_idTipoDocumento" + "('" + id + "')";
                NpgsqlDataAdapter da = new NpgsqlDataAdapter(sql, compConexion);
                // i always reset DataSet before i do something with it.... i don't know why :-)
                ds.Reset();
                // filling DataSet with result from NpgsqlDataAdapter
                da.Fill(ds);
                // since it C# DataSet can handle multiple tables, we will select first
                dt = ds.Tables[0];
            }
            catch (Exception e)
            {
                throw new Exception(e.Message + "\nError al obtener los atributos");
            }
            finally
            {
                try
                {
                    compConexion.Close();
                }
                catch
                {

                }
            }
            string[] valores;
            foreach (DataRow row in dt.Rows)
            {
                valores = datosDataSet(row, 5);
                try
                {
                    nueva.AddLast(new atributo(valores[0], valores[1], valores[2], bool.Parse(valores[3]), int.Parse(valores[4])));
                }
                catch { }
                
            }
            return nueva;
        }

        #endregion

        #endregion

        public override object obtenerEstado()
        {
            throw new NotImplementedException();
        }
    }
}
